package com.transcoachapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.transcoachapp.BusStations;
import com.transcoachapp.R;
import com.transcoachapp.RailStations;
import com.transcoachapp.data.Rail;

public class RailAdapter extends ArrayAdapter<Rail>{
	private Activity context;
	private ArrayList<Rail> items;
	
	public RailAdapter(Context context, int textViewResourceId,
			ArrayList<Rail> objects) { 
		super(context, textViewResourceId);
		this.items = objects;
		this.context = (Activity) context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (this.items != null)
		      return this.items.size();
		return 0;
	}
	@Override
	public Rail getItem(int arg0) {
		// TODO Auto-generated method stub
		return this.items.get(arg0);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
		
		final Rail rail  = getItem(position);
		View bsdRow = context.getLayoutInflater().inflate(R.layout.rail_line, null);
		TextView textView = (TextView) bsdRow.findViewById(R.id.railName);
        textView.setText(rail.getCode() + " - "+ rail.getName());
	    bsdRow.setClickable(true);
        bsdRow.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent intent = new Intent();								
                intent.setClass(context, RailStations.class);
                intent.putExtra("type", rail.getType());
                intent.putExtra("railCode", rail.getCode());
                intent.putExtra("railName", rail.getName());
				context.startActivity(intent);	
			}
		});

		return bsdRow;
	}
}
